+"""Xend interface to the vifctl script.
+"""
import os
import os.path
import sys
VIFCTL = '/etc/xen/xend/vifctl'
def init():
+ """Call 'vifctl init'. Called when xend starts.
+ """
os.system(VIFCTL + ' init ')
-def up(vif, mac=None, bridge=None, ipaddr=[]):
+def vifctl_args(vif, mac=None, bridge=None, ipaddr=[]):
+ """Construct the argument list for vifctl.
+ """
args = ['vif=%s' % vif]
if mac:
args.append('mac=%s' % mac)
if bridge:
args.append('bridge=%s' % bridge)
- if ipaddr:
- args.append('ipaddr=%s' % ','.join(ipaddr))
- os.system(VIFCTL + ' up ' + ' '.join(args))
+ for ip in ipaddr:
+ args.append('ipaddr=%s' % ip)
+ return ' '.join(args)
+
+def up(vif, **kwds):
+ """Call 'vifctl up' for a vif. Called when a vif is created.
+ """
+ args = vifctl_args(vif, **kwds)
+ os.system(VIFCTL + ' up ' + args)
-def down(vif, mac=None, bridge=None, ipaddr=[]):
- args = ['vif=%s' % vif]
- if mac:
- args.append('mac=%s' % mac)
- if bridge:
- args.append('bridge=%s' % bridge)
- if ipaddr:
- args.append('ipaddr=%s' % ','.join(ipaddr))
- os.system(VIFCTL + ' down ' + ' '.join(args))
+def down(vif, **kwds):
+ """Call 'vifctl down' for a vif. Called when a vif is destroyed.
+ """
+ args = vifctl_args(vif, **kwds)
+ os.system(VIFCTL + ' down ' + args)
"child2",
"child3",
"child4",
- "child_value",
+ "child_value",
"has_id",
"with_id",
"child_with_id",
"elements",
+ "to_string",
+ "from_string",
+ "all_from_string",
"parse",
]
self.state_start(c)
def atomp(sxpr):
+ """Check if an sxpr is an atom.
+ """
if sxpr.isalnum() or sxpr == '@':
return 1
for c in sxpr:
return 1
def show(sxpr, out=sys.stdout):
+ """Print an sxpr in bracketed (lisp-style) syntax.
+ """
if isinstance(sxpr, types.ListType):
out.write(k_list_open)
i = 0
out.write(repr(str(sxpr)))
def show_xml(sxpr, out=sys.stdout):
+ """Print an sxpr in XML syntax.
+ """
if isinstance(sxpr, types.ListType):
element = name(sxpr)
out.write('<%s' % element)
out.write(str(sxpr))
def elementp(sxpr, elt=None):
+ """Check if an sxpr is an element of the given type.
+
+ sxpr sxpr
+ elt element type
+ """
return (isinstance(sxpr, types.ListType)
and len(sxpr)
and (None == elt or sxpr[0] == elt))
def name(sxpr):
+ """Get the element name of an sxpr.
+ If the sxpr is not an element (i.e. it's an atomic value) its name
+ is None.
+
+ sxpr
+
+ returns name (None if not an element).
+ """
val = None
if isinstance(sxpr, types.StringType):
val = sxpr
return val
def attributes(sxpr):
+ """Get the attribute list of an sxpr.
+
+ sxpr
+
+ returns attribute list
+ """
val = []
if isinstance(sxpr, types.ListType) and len(sxpr) > 1:
attr = sxpr[1]
return val
def attribute(sxpr, key, val=None):
+ """Get an attribute of an sxpr.
+
+ sxpr sxpr
+ key attribute key
+ val default value (default None)
+
+ returns attribute value
+ """
for x in attributes(sxpr):
if x[0] == key:
val = x[1]
return val
def children(sxpr, elt=None):
+ """Get children of an sxpr.
+
+ sxpr sxpr
+ elt optional element type to filter by
+
+ returns children (filtered by elt if specified)
+ """
val = []
if isinstance(sxpr, types.ListType) and len(sxpr) > 1:
i = 1
return val
def child(sxpr, elt, val=None):
+ """Get the first child of the given element type.
+
+ sxpr sxpr
+ elt element type
+ val default value
+ """
for x in children(sxpr):
if elementp(x, elt):
val = x
return val
def child_at(sxpr, index, val=None):
+ """Get the child at the given index (zero-based).
+
+ sxpr sxpr
+ index index
+ val default value
+ """
kids = children(sxpr)
if len(kids) > index:
val = kids[index]
return val
def child0(sxpr, val=None):
+ """Get the zeroth child.
+ """
return child_at(sxpr, 0, val)
def child1(sxpr, val=None):
+ """Get the first child.
+ """
return child_at(sxpr, 1, val)
def child2(sxpr, val=None):
+ """Get the second child.
+ """
return child_at(sxpr, 2, val)
def child3(sxpr, val=None):
+ """Get the third child.
+ """
return child_at(sxpr, 3, val)
def child4(sxpr, val=None):
+ """Get the fourth child.
+ """
return child_at(sxpr, 4, val)
def child_value(sxpr, elt, val=None):
+ """Get the value of the first child of the given element type.
+ Assumes the child has an atomic value.
+
+ sxpr sxpr
+ elt element type
+ val default value
+ """
kid = child(sxpr, elt)
if kid:
val = child_at(kid, 0, val)
returns sxpr
"""
io = StringIO(str)
- return parse(io)
+ vals = parse(io)
+ if vals is []:
+ return None
+ else:
+ return vals[0]
+
+
+def all_from_string(str):
+ """Create an sxpr list by parsing a string.
+
+ str string
+ returns sxpr list
+ """ io = StringIO(str)
+ vals = parse(io)
+ return vals
def parse(io):
"""Completely parse all input from 'io'.